package com.example.exceldemo.liteflowComponent.gs;

import cn.hutool.core.date.DateUtil;
import com.deepoove.poi.config.Configure;
import com.example.exceldemo.constants.ReqMgrConstants;
import com.example.exceldemo.liteflowComponent.common.GenerateDocuments;
import com.example.exceldemo.model.DemoContext;
import com.example.exceldemo.model.FunctionInfoDTO;
import com.example.exceldemo.model.ReqAnalysisSpecificationDTO;
import com.example.exceldemo.model.ReqInfoDTO;
import com.example.exceldemo.util.WordUtil;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import lombok.extern.slf4j.Slf4j;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 生成需求分析文档
 * @author yangwb8
 * @Date 2025/5/17 04:06
 */
@LiteflowComponent("one")
@Slf4j
public class GenerateOneDocuments extends GenerateDocuments {
    @Override
    public void processReqDocuments(ReqInfoDTO reqInfo, String templateDir, String reqOutputDir, Map<String, String> settings) {
        log.info("开始生成需求分析文档");
        //
        ReqAnalysisSpecificationDTO reqAnalysisDTO = prepareReqAnalysisData(reqInfo, settings);
        DemoContext deoContext = getDemoContext();
        deoContext.setReqAnalysisDTO(reqAnalysisDTO);

        Configure baseConfig = createBaseConfig(getPolicy());

        // 修改：直接使用模板文件名，无需构建完整路径
        String templateFileName = ReqMgrConstants.TEMPLATE_ANALYSIS;
        Path outputPath = Paths.get(reqOutputDir, getDocName(reqInfo));

        String provCode = reqInfo.getProvCode();
        // 统一使用类路径方式生成文档
        WordUtil.generateWordFileByOS(provCode,templateFileName, outputPath.toString(), reqAnalysisDTO, baseConfig);
        log.info("需求分析文档生成完成");
    }

    @Override
    public String getDocName(ReqInfoDTO reqInfo) {
        String reqName = reqInfo.getReqName();
        return ReqMgrConstants.DOCUMENT_PREFIX + reqName + "的需求分析说明书.docx";
    }

    /**
     * 准备需求分析说明书数据
     *
     * @param reqInfo  需求信息
     * @param settings 配置信息
     * @return 需求分析说明书数据对象
     */
    private  ReqAnalysisSpecificationDTO prepareReqAnalysisData(ReqInfoDTO reqInfo, Map<String, String> settings) {
        ReqAnalysisSpecificationDTO reqAnalysisDTO = new ReqAnalysisSpecificationDTO();
        LocalDateTime now = LocalDateTime.now();

        // 设置文档元信息
        reqAnalysisDTO.setAuthor(Optional.ofNullable(reqInfo.getReqDeveloper()).orElse(""));
        reqAnalysisDTO.setCrtDate(DateUtil.format(now, ReqMgrConstants.DATE_FORMAT));
        reqAnalysisDTO.setVersion(settings.getOrDefault("version", ReqMgrConstants.DEFAULT_VERSION));
        reqAnalysisDTO.setAuditor(settings.getOrDefault("defaultAuditor", "范鹏举"));
        reqAnalysisDTO.setAuditDate(DateUtil.format(now, ReqMgrConstants.DATE_FORMAT));
        reqAnalysisDTO.setMouth(DateUtil.format(now, ReqMgrConstants.MONTH_FORMAT));

        // 设置需求基本信息
        reqAnalysisDTO.setReqNo(reqInfo.getReqNo());
        reqAnalysisDTO.setReqName(reqInfo.getReqName());
        reqAnalysisDTO.setReqDesc(Optional.ofNullable(reqInfo.getReqDesc()).orElse(""));
        reqAnalysisDTO.setAnalysts(settings.getOrDefault("defaultAuthor", "杨文波"));
        reqAnalysisDTO.setAnalysisTime(DateUtil.format(now, ReqMgrConstants.DATE_FORMAT));
        reqAnalysisDTO.setReqOwner(Optional.ofNullable(reqInfo.getReqOwner()).orElse(""));
        reqAnalysisDTO.setReqDeveloper(Optional.ofNullable(reqInfo.getReqDeveloper()).orElse(""));

        // 安全地处理需求提出时间（避免空指针异常）
        reqAnalysisDTO.setReqProposeTime(
                Optional.ofNullable(reqInfo.getReqProposeTime())
                        .map(time -> DateUtil.format(time, ReqMgrConstants.DATE_FORMAT))
                        .orElse(DateUtil.format(now, ReqMgrConstants.DATE_FORMAT))
        );

        // 设置需求重要性和紧急程度
        reqAnalysisDTO.setIsKey(settings.getOrDefault("isKey", "否"));
        reqAnalysisDTO.setUrgency(settings.getOrDefault("urgency", "一般"));

        // 安全地处理预计上线时间
        reqAnalysisDTO.setExpectOnlineTime(
                Optional.ofNullable(reqInfo.getExpectOnlineTime())
                        .map(time -> DateUtil.format(time, ReqMgrConstants.DATE_FORMAT))
                        .orElse(DateUtil.format(now.plusMonths(1), ReqMgrConstants.DATE_FORMAT))
        );

        // 设置其他信息
        reqAnalysisDTO.setMenu(reqInfo.getMenu());
        reqAnalysisDTO.setBusiness(reqInfo.getBusiness());
        reqAnalysisDTO.setTestUserName(reqInfo.getTestUserName());
        reqAnalysisDTO.setWorkingTotalHours("");  // 总工时（留空）
        reqAnalysisDTO.setIsUrgent(settings.getOrDefault("isUrgent", "否"));  // 是否紧急



        // STEP 4: 处理功能列表
        List<FunctionInfoDTO> functionList = processFunctionList(reqInfo);
        reqAnalysisDTO.setFunctionList(functionList);

        return reqAnalysisDTO;
    }
}
